<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Cat;
use DB;

class CatController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $cats = DB::table('cats')->get();
        $cats =$this->findAll();
        // dd($cats);
        $num = count($cats);
        return view('admin.cat_index')->with('cats',$cats)->with('num',$num);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        $cats =$this->findAll();
        return view('admin.cat_add')->with('cats',$cats);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 缩略图上传
        // 上传的判断文件是否有效
        if($request->file('catpic')->isValid()){
            // 获取上传的文件对象
            $data = $request->file('catpic');
            // 获取上传的文件的后缀
            $ext = $data->getClientOriginalExtension();
            // 拼装出你需要使用的文件名
            $picname = time().rand(1000,9999).'.'.$ext;
            // 移动临时文件，生成新文件到指定目录下
            $data->move(public_path('upload/images/'),$picname);
        }
        // dd($data);
        // dd($request->parent_id);
        //验证
        $cat = new Cat();
        $cat->parentid=$request->parentid;
        $cat->catname = $request->catname;
        $cat->catpic = $picname;
        $cat->save();
        return redirect('admin/cat');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // 获取数据

        $cats = DB::table('cats')->where('catid',$id)->first();
        return view('admin.cat_edit')->with('cats',$cats);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if($request->hasFile('catpic')){
            // 上传的判断文件是否有效
            if($request->file('catpic')->isValid()){
                // 获取上传的文件对象
                $data = $request->file('catpic');
                // 获取上传的文件的后缀
                $ext = $data->getClientOriginalExtension();
                // 拼装出你需要使用的文件名
                $picname = time().rand(1000,9999).'.'.$ext;
                // 移动临时文件，生成新文件到指定目录下
                $data->move(public_path('upload/images/'),$picname);
            }
            $catname = $request->catname;
            $catpic = $picname;
            DB::table('cats')->where('catid',$request->catid)->update(['catname'=>$catname,'catpic'=>$catpic]);
        }else{
            $catname = $request->catname;
            DB::table('cats')->where('catid',$request->catid)->update(['catname'=>$catname]);
        }
          
        return redirect('admin/cat');
        
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    // 删除栏目
    public function delete($id)
    {
        $data = DB::table('cats')->where('catid',$id)->first();  
        if($data->parentid == 0){
            DB::table('cats')->where('parentid',$id)
            ->orWhere('catid',$id)
            ->delete();
            return redirect('admin/cat');
        }else{
            DB::table('cats')->where('catid',$id)
            ->delete();
            return redirect('admin/cat');
            }
            
    }


    /**
     * 找子孙栏目
     * @param $data  栏目数据源
     * @param int $id
     * @param int $lev
     * @return array
     */
    protected  function  findTree($data,$id=0,$lev=0)
    {
        static $tree=[];
        foreach($data as $k=>$v){
            if($v->parentid==$id){
                $v->lev=$lev;
                $tree[] = $v;
                $this->findTree($data,$v->catid,$lev+1);
            }
        }
        return $tree;
    }
    //格式化取出所有栏目
    protected function findAll()
    {


        $data = Cat::select('catid','parentid','catname','catpic')->get();
        return  $this->findTree($data,0,0);
    }

}
